class UserMailer < ActionMailer::Base
  def signup_notification(user)
    setup_email(user)
    @subject    += 'Please activate your new account'
    @body[:url]  = App[:website]+"/activate?activation_code=#{user.activation_code}"

  end

  def activation(user)
    @recipients  = "#{user.email}"
    @from        = App[:from]
    @subject     = "[wangzhen] "
    @sent_on     = Time.now
    @body[:user] = user
    @subject    += 'Your account has been activated!'
    @body[:url]  = App[:website]
    @sent_on     = Time.now
  end

  def user_update(user)
    setup_email(user)
    @subject    += '用户信息已更改'
    @body[:url]  = App[:website]
    @sent_on     = Time.now
  end


  # Delivery notification mail generated by "Contact" module.
  # Note:
  # The only parameter "mail" should be an instance of "Contact" model in "Contact"
  # module.
  def  contact(contact)
    @recipients = App[:recipients]
    @body[:contact] = contact
    @from =  App[:from]
    @subject ="咨询"+ contact.name
    @sent_on     = Time.now
  end


  def  contact_to_user(contact)
    @recipients = contact.email
    @body[:contact] = contact
    @body[:content] = contact.content
    @body[:tel] = contact.phone
    @body[:name] = contact.name
    @body[:email] = contact.email
    @body[:serial] = contact.serial
    @from =  App[:from]
    @subject ='谢谢您的咨询，我们会尽快回复！'
    @sent_on     = Time.now
  end

  def  order(order)
    @recipients = order.email,  App[:recipients]
    @from =  App[:from]
    @subject ="order"
    @sent_on     = Time.now
  end

  def  adv(recipient)
    @recipients = recipient
    @body[:products] =  Product.find(:all ,:limit => 8)
    @from =  "vwangzhen@126.com"
    @subject ="翡翠网上商城"
    @sent_on     = Time.now
  end

  protected
  def setup_email(user)
    initialize_setting
    @recipients  = "#{user.email}"
    @from        = App[:from]
    @subject     = "[wangzhen] "
    @sent_on     = Time.now
    @body[:user] = user
  end
  
  private
  def initialize_setting
    if false
      setting = MailSetting.find_by_status(true)
      ActionMailer::Base.smtp_settings = {
        :address => setting.address,
        :domain => setting.domain,
        :user_name => setting.user_name,
        :password => setting.password,
        :authentication => setting.authentication.to_sym ,
        :port => setting.port ,
        :enable_starttls_auto => setting.enable_starttls_auto
      }
      ActionMailer::Base.smtp_settings[:port] =setting.port unless setting.port.blank?
      ActionMailer::Base.smtp_settings[:enable_starttls_auto] = setting.enable_starttls_auto. unless setting.enable_starttls_auto.blank?
    else
      ActionMailer::Base.smtp_settings = {
        :address => "smtp.gmail.com",
        :port => 587,
        :authentication => :plain,
        :enable_starttls_auto => true,
        :user_name =>  'haofeicui8@gmail.com',
        :password =>  'hfchfc888'
      }
    end
    ActionMailer::Base.default_content_type = "text/html"
    ActionMailer::Base.default_implicit_parts_order =["text/html", "text/enriched", "text/plain"]
    ActionMailer::Base.default_charset  = 'utf-8'
    ActionMailer::Base.raise_delivery_errors = false
  end
end
